{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 语法：if ...... [elif ...]  [else ...]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2 > 1 成立\n"
     ]
    }
   ],
   "source": [
    "if 2 > 1 :\n",
    "    print('2 > 1 成立')   #强烈推荐使用四个空格作为缩进"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2 < 1 不成立\n"
     ]
    }
   ],
   "source": [
    "if 2 < 1 :\n",
    "    print('2 < 1 成立')\n",
    "else:\n",
    "    print('2 < 1 不成立')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "elif True 进入\n"
     ]
    }
   ],
   "source": [
    "# 只会进入一条\n",
    "if False:\n",
    "    print('if False 进入')\n",
    "elif True:\n",
    "    print('elif True 进入')\n",
    "else:\n",
    "    print('else 进入')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "if1 2>1\n",
      "if1 3>2\n",
      "if2 2>1\n"
     ]
    }
   ],
   "source": [
    "if 2>1:\n",
    "    print('if1 2>1')\n",
    "if 3>2:\n",
    "    print('if1 3>2')\n",
    "\n",
    "# 条件判断，只会执行一条。下面的不再执行了\n",
    "if 2>1:  # if和elif的对比判断\n",
    "    print('if2 2>1')\n",
    "elif 3>2:\n",
    "    print('if2 3>2')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## if判断的值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "|通过验证|True|1|-1|有值字符串|有值列表|||\n",
    "|:-:|:-:|:-:|:-:|:-:|:-:|:-:|\n",
    "|不通过验证|False|0||空字符串|空列表||||"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 2 > 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2 > 1 成立\n"
     ]
    }
   ],
   "source": [
    "if a:\n",
    "    print('2 > 1 成立')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "无法通过\n"
     ]
    }
   ],
   "source": [
    "if 0:  # 测试0是否可以通过\n",
    "    print('通过')\n",
    "else:\n",
    "    print('无法通过')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "通过\n"
     ]
    }
   ],
   "source": [
    "if 1:  # 测试0是否可以通过\n",
    "    print('通过')\n",
    "else:\n",
    "    print('无法通过')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "通过\n"
     ]
    }
   ],
   "source": [
    "if -1:  # 测试0是否可以通过\n",
    "    print('通过')\n",
    "else:\n",
    "    print('无法通过')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "无法通过\n"
     ]
    }
   ],
   "source": [
    "if '':  # 测试0是否可以通过\n",
    "    print('通过')\n",
    "else:\n",
    "    print('无法通过')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "通过\n"
     ]
    }
   ],
   "source": [
    "if ' ':  # 测试0是否可以通过\n",
    "    print('通过')\n",
    "else:\n",
    "    print('无法通过')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "通过\n"
     ]
    }
   ],
   "source": [
    "if 'sdf23':  # 测试0是否可以通过\n",
    "    print('通过')\n",
    "else:\n",
    "    print('无法通过')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "无法通过\n"
     ]
    }
   ],
   "source": [
    "if []:  # 测试0是否可以通过\n",
    "    print('通过')\n",
    "else:\n",
    "    print('无法通过')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "通过\n"
     ]
    }
   ],
   "source": [
    "if [1]:  # 测试0是否可以通过\n",
    "    print('通过')\n",
    "else:\n",
    "    print('无法通过')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 更多的语法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "通过\n"
     ]
    }
   ],
   "source": [
    "if not 0:  # not 反向判断值\n",
    "    print('通过')\n",
    "else:\n",
    "    print('无法通过')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "无法通过\n"
     ]
    }
   ],
   "source": [
    "if 2 > 1 and 3 < 2:  # and 条件需要同时成立\n",
    "    print('通过')\n",
    "else:\n",
    "    print('无法通过')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "通过\n"
     ]
    }
   ],
   "source": [
    "if 2 > 1 or 3 < 2:  # or 多条件，通过一个即可\n",
    "    print('通过')\n",
    "else:\n",
    "    print('无法通过')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
